package com.douqu.game.core.entity.ext;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.WorldInfo;
import com.douqu.game.core.entity.battle.BattleRecord;
import com.douqu.game.core.entity.sprite.Player;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by bean on 2017/7/20.
 */
public class BattleInfo extends ExtInfo {


    /**
     * 战斗记录，最新的排在最后
     */
    private List<BattleRecord> records;

    public BattleInfo(Player player, WorldInfo worldInfo) {
        super(player, worldInfo);

        records = new CopyOnWriteArrayList<>();
    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
//        int size = records.size();
//        buffer.writeShort(size);
//        for(BattleRecord record : records)
//        {
//            record.writeTo(buffer);
//        }
//
//        buffer.writeInt(0);//备用
    }

    @Override
    public void loadFrom(ByteBuffer buffer)
    {
//        int size = buffer.readShort();
//        BattleRecord record = null;
//        for(int i = 0; i < size; i++)
//        {
//            record = new BattleRecord();
//            record.loadFrom(buffer);
//            records.add(record);
//        }
    }


    public void addRecord(BattleRecord record)
    {
        records.add(record);
    }

    public int getRecordCount()
    {
        return records.size();
    }


    /**
     * 是否两连败
     * @return
     */
    public boolean isTwoFail()
    {
        int start = records.size() - 1;
        if(start <= 0)
            return false;

        int end = start - 1;
        end = end < 0 ? 0 : end;
        if(start == end)
            return false;

        for(int i = start; i >= end; i--)
        {
            if(!records.get(i).isLose())
                return false;
        }
        return true;
    }


    /**
     * 获取上一场匹配的目标
     * @return
     */
    public String getLastTarget(int... battleType)
    {
        int size = records.size();
        for(int i = size - 1; i >= 0; i--)
        {
            for(int type : battleType)
            {
                if(records.get(i).getBattleType() == type)
                    return records.get(i).getTargetIndex();
            }
        }

        return null;
    }




}
